package process;

import cn.itcast.flink.process.FlinkSQL_Table_Demo03;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @author ChinaManor
 * #Description Table_Demo03
 * #Date: 22/6/2021 21:11
 */
public class Table_Demo03 {
    public static void main(String[] args) throws Exception {
        //1.准备环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

        //2.Source
        DataStream<FlinkSQL_Table_Demo03.WC> input = env.fromElements(
                new FlinkSQL_Table_Demo03.WC("Hello", 1),
                new FlinkSQL_Table_Demo03.WC("World", 1),
                new FlinkSQL_Table_Demo03.WC("Hello", 1)
        );

        Table table = tEnv.fromDataStream(input);
        Table resultTable = table
                .groupBy("word")
                .select($("word"), $("frequency").sum().as("frequency"));
        DataStream<Tuple2<Boolean, FlinkSQL_Table_Demo03.WC>> stream = tEnv.toRetractStream(resultTable, FlinkSQL_Table_Demo03.WC.class);
        stream.print();
        env.execute();
    }
}
